﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Threading;
using System.Globalization;
using System.Resources;
using EHTWebsite.Code;
using System.Text;
using System.IO;
using System.Net;
using System.Web.Security;
using EHTWebsiteProject.Code.Entity;
using EHTWebsiteProject.Code.BLL;

namespace EHTWebsiteProject.Administrator
{
    public partial class EHTMediaFilesUI : System.Web.UI.Page
    {
        public Boolean IsVN = true;
        private String languageCode = "vi-VN";
        private Int32 FileID = HangSo.DEFAULT_ID;
        private String message = "";
        private ResourceManager rm = new ResourceManager("Resources.SiteMap", System.Reflection.Assembly.Load("App_GlobalResources"));

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                try
                {
                    languageCode = Session[HangSo.LANGUAGE_CODE].ToString();
                    if (languageCode == "en-us")
                    {
                        IsVN = false;
                    }

                }
                catch (Exception ex)
                {
                }

                try
                {
                    FileID = Int32.Parse(Request.QueryString["FileID"]);
                }
                catch
                {

                }

                try
                {
                    message = Request.QueryString["Message"].ToString();
                    if (message != "")
                        ltrErorr.Text = HelpMethod.ApplySuccess(message);
                }
                catch
                {

                }
                Thread.CurrentThread.CurrentCulture = new CultureInfo(languageCode);
                LoadLanguage(Thread.CurrentThread.CurrentCulture);
                if (FileID != -1)
                {
                    ViewState["action"] = HangSo.ACTION_UPDATE;
                    ViewState["FileID"] = FileID;
                    if (IsVN == true)
                        lbAdd_Edit.Text = HangSo.Edit_VN;
                    else
                        lbAdd_Edit.Text = HangSo.Edit_Other;
                    EHTWebsite.Code.File theFile = FileProvider.GetFileByFileID(FileID);
                    txtMoTa.Text = theFile.MoTaVN;
                    txtMotaOther.Text = theFile.MoTaOther;
                    ViewState["FileUrl"] = theFile.FileUrl;
                }
                else
                {
                    ViewState["action"] = HangSo.ACTION_ADD;
                    if (IsVN == true)
                        lbAdd_Edit.Text = HangSo.Add_VN;
                    else
                        lbAdd_Edit.Text = HangSo.App_Other;
                }
            }
        }

        /// <summary>
        /// Ham load ngon ngu
        /// </summary>
        /// <param name="ci"></param>
        public void LoadLanguage(CultureInfo ci)
        {
            lbApply.Text = rm.GetString("lbApply", ci);          
            lbHelp.Text = rm.GetString("lbHelp", ci);
            lbSave.Text = rm.GetString("lbSave", ci);
            lbCancel.Text = rm.GetString("lbCancel", ci);
            lbMota.Text = rm.GetString("lbMota", ci);
            lbMotaOther.Text = rm.GetString("lbMotaOther", ci);            
        }

        public Boolean ValidateData()
        {
            Boolean flag = true;
            if (txtMoTa.Text == "")
            {
                if (IsVN == true)
                    message = HangSo.check_VN;
                else
                    message = HangSo.check_Other;
                flag = false;
            }
            //if (txtMotaOther.Text == "")
            //{
            //    if (IsVN == true)
            //        message = HangSo.check_VN;
            //    else
            //        message = HangSo.check_Other;
            //    flag = false;
            //}

            string sExtension = ".doc,.docx,.pdf,.xlsx,.xls,.txt,.DOC,.DOCX,.XLS,.XLSX,.TXT,.PDF";

            try
            {
                if (ViewState["action"].ToString() == HangSo.ACTION_ADD)
                {
                    if (!this.fulFiles.HasFile)
                    {
                        flag = false;
                        if (IsVN == true)
                            message = HangSo.Choose_File;
                        else
                            message = HangSo.Choose_FileOther;
                    }
                    else
                    {
                        if (sExtension.IndexOf(Path.GetExtension(fulFiles.PostedFile.FileName)) < 0)
                        {
                            flag = false;
                            if(IsVN==true)
                                message = HangSo.Erorr_File;
                            else
                                message = HangSo.Erorr_FileOther;
                        }

                    }
                }
                else
                {
                    if (this.fulFiles.HasFile && (sExtension.IndexOf(Path.GetExtension(fulFiles.PostedFile.FileName)) < 0))
                    {
                        flag = false;
                        if (IsVN == true)
                            message = HangSo.Erorr_File;
                        else
                            message = HangSo.Erorr_FileOther;
                    }
                }

            }
            catch (ApplicationException ex)
            {
                throw new ApplicationException(ex.Message);

            }

            return flag;
        }

        protected void ImgSave_Click(object sender, ImageClickEventArgs e)
        {
            String action = ViewState["action"].ToString();
            Int32 FileIDOut = 0;
            if (ValidateData() == false)
            {
                ltrErorr.Text = HelpMethod.ErorrLogin(message);
                return;
            }
            else
            {
                EHTWebsite.Code.File theFile = new EHTWebsite.Code.File();
                theFile.MoTaOther = txtMotaOther.Text;
                theFile.MoTaVN = txtMoTa.Text;
                theFile.TrangThai = true;
                theFile.SapXep = 1;
                if (this.fulFiles.HasFile)
                {
                    String fileName = fulFiles.PostedFile.FileName;
                    String fileType = Path.GetExtension(fileName);
                    String newFileName = DateTime.Now.Ticks.ToString() + fileType;
                    fulFiles.PostedFile.SaveAs(Server.MapPath("~/") + "//ImageDatas//Files//" + newFileName);
                    theFile.FileUrl = newFileName;
                }
                else
                    theFile.FileUrl = ViewState["FileUrl"].ToString();
                if (action == HangSo.ACTION_ADD)
                {
                    theFile.FileID = -1;
                    FileIDOut = FileProvider.IUFile(theFile, true);
                }
                else
                {
                    theFile.FileID = Int32.Parse(ViewState["FileID"].ToString());
                    FileIDOut =  FileProvider.IUFile(theFile, false);
                }
            }
            Response.Redirect("EHTMediaFiles.aspx");
            
        }

        protected void ImgApply_Click(object sender, ImageClickEventArgs e)
        {
            String action = ViewState["action"].ToString();
            Int32 FileIDOut = 0;
            if (ValidateData() == false)
            {
                ltrErorr.Text = HelpMethod.ErorrLogin(message);
                return;
            }
            else
            {
                EHTWebsite.Code.File theFile = new EHTWebsite.Code.File();
                theFile.MoTaOther = txtMotaOther.Text;
                theFile.MoTaVN = txtMoTa.Text;
                theFile.TrangThai = true;
                theFile.SapXep = 1;

                if (this.fulFiles.HasFile)
                {
                    String fileName = fulFiles.PostedFile.FileName;
                    String fileType = Path.GetExtension(fileName);
                    String newFileName = DateTime.Now.Ticks.ToString() + fileType;
                    fulFiles.PostedFile.SaveAs(Server.MapPath("~/") + "//ImageDatas//Files//" + newFileName);
                    theFile.FileUrl = newFileName;
                }
                else
                    theFile.FileUrl = ViewState["FileUrl"].ToString();
                if (action == HangSo.ACTION_ADD)
                {
                    theFile.FileID = -1;
                    FileIDOut = FileProvider.IUFile(theFile, true);
                }
                else
                {
                    theFile.FileID = Int32.Parse(ViewState["FileID"].ToString());
                    FileIDOut = FileProvider.IUFile(theFile, false);
                }
            }
            String str = "";
            if (IsVN == true)
                str = HangSo.App_VN;
            else
                str = HangSo.App_Other;
            Response.Redirect("EHTMediaFilesUI.aspx?FileID="+FileIDOut+"&&Message="+str);
        }
    }
}